package com.yuanrui.subject.domain.handler.subject;

import com.yuanrui.subject.common.enums.IsDeletedFlagEnum;
import com.yuanrui.subject.common.enums.SubjectInfoTypeEnum;
import com.yuanrui.subject.domain.convert.SubjectRadioConvert;
import com.yuanrui.subject.domain.entity.SubjectAnswerBO;
import com.yuanrui.subject.domain.entity.SubjectInfoBO;
import com.yuanrui.subject.domain.entity.SubjectOptionBO;
import com.yuanrui.subject.infra.basic.entity.SubjectRadio;
import com.yuanrui.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //校验题目信息是否为空
        if (subjectInfoBO.getOptionList()==null || subjectInfoBO.getOptionList().isEmpty()){
            return;
        }
        //单选题目的添加逻辑，将所有选项都添加到选项表中
        List<SubjectRadio> radios = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = SubjectRadioConvert.INSTANCE.convertToBoEntity(option);
            subjectRadio.setIsCorrect(option.getIsCorrect());
            subjectRadio.setOptionContent(option.getOptionContent());
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            radios.add(subjectRadio);
        });

        subjectRadioService.batchInsert(radios);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectRadio> subjectRadios = subjectRadioService.queryBySujectId(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOS = SubjectRadioConvert.INSTANCE.convertToAnswerBo(subjectRadios);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }

    @Override
    public SubjectInfoBO queryAll(Long subjectId) {
        return null;
    }
}
